<?php

include_once "../../../include/cp_header.php";
global $xoopsUser, $xoopsConfig, $xoopsModuleConfig, $xoopsModule;
global $xoopsDB;

require_once "../include/functions.inc"; 
require_once "functions.inc"; 

// clean data passed to the form
$cleanPost = cleanInput($_POST);
$submit = $cleanPost['submit'];

require_once('../class/tcpdf/config/lang/eng.php');
require_once('../class/tcpdf/tcpdf.php');

$Hndl =& $xoopsDB->query("SELECT *" .
						 " FROM " . $xoopsDB->prefix("confreg_conferences") .
						 " WHERE conf_id ='" . intval($cleanPost['conf_id']) . "'" );
$conf = $xoopsDB->fetchArray($Hndl);

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, 'LETTER', true, 'UTF-8', false); 

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Mid-Continent MicroStation Community');
$pdf->SetTitle($conf['title']);
$pdf->SetSubject($conf['title']);
$pdf->SetKeywords(str_replace(' ', ',', $conf['title']));

// set default header data
$pdf->SetHeaderData('logo_banner.gif', PDF_HEADER_LOGO_WIDTH, $conf['title'], '');

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 

//set some language-dependent strings
$pdf->setLanguageArray($l); 

// ---------------------------------------------------------

// set font
$pdf->SetFont('dejavusans', '', 10);


// output the HTML content
$pdf_report = generate_pdf(intval($cleanPost['conf_id']));
$page_pos = stripos($pdf_report, '<h3>workshop information</h3>');
$page1 = substr($pdf_report,0,$page_pos);
$page2 = substr($pdf_report,$page_pos,strlen($pdf_report));

$pdf->AddPage();
$pdf->lastPage();
$pdf->writeHTML($page1, true, 0, true, 0);

$pdf->AddPage();
$pdf->lastPage();
$pdf->writeHTML($page2, true, 0, true, 0);

$pdf->lastPage();

$pdf->Output('tmcmcmc_report.pdf', 'I');

exit;

function generate_pdf($conf_id) {
	global $xoopsDB;
	$groups = get_groups_info($conf_id);

	$html_output = "<h3>Group Information</h3>\n<table>\n<tr>\n<th>Company</th><th>Attendees</th><th>Cost</th><th>Paid</th></tr>\n";
	if (count($groups) > 0) {
		foreach ($groups as $thisGroup) {
			$html_output .= "<tr>";
			$html_output .= '<td>' . $thisGroup['company'] . '</td>';
			$html_output .= '<td>' . $thisGroup['total_count'] . '</td>';
			$html_output .= '<td>$' . money_format('%.2n',$thisGroup['total_cost']) . '</td>';
			$html_output .= '<td>$' . money_format('%.2n',$thisGroup['paid']) . '</td>';
			$html_output .= "</tr>\n";
		}
	} else {
			$html_output .= "<tr><td colspan=5>No groups defined yet.</td></tr>\n";
	}

	$html_output .= "</table>\n<p><h3>Coupons</h3>\n";
 	$coupons = get_coupon_info($conf_id);

 	if (count($coupons) > 0) {
 		$cell = '<ul>';
 		foreach ($coupons as $coup) {
 			$cell .= '<li style="font-weight:bold; color: #2F5376;">' . $coup['code'] . ' (' . $coup['count'] . ') - '. $coup['description'] .'</li><ul type="disc">';
 			foreach ($coup['attendee'] as $user) {
 				$cell .= '<li>' . $user . '</li>';
 			}
 			$cell .= '</ul>';
 		}
 		$cell .= '</ul>';
 		$html_output .= $cell;
 	} else {
 		$html_output .= "No coupons defined yet.";
 	}

	$html_output .= "\n<h3>Extra Information</h3>\n";
	$extras = get_extra_info($conf_id);
	
	if (count($extras) > 0) {
		$cell = '<ul>';
		foreach ($extras as $thisEx) {
			$cell .= '<li style="font-weight:bold; color: #2F5376;">' . $thisEx['name'] . ' (' . $thisEx['count'] . ')</li><ul type="disc">';
			foreach ($thisEx['attendee'] as $user) {
				$cell .= '<li>' . $user . '</li>';
			}
			$cell .= '</ul>';
		}
		$cell .= '</ul>';
		$html_output .= $cell;
	} else {
		$html_output .= "No extras defined yet.";
	}

	$html_output .= "\n<h3>Workshop Information</h3>\n";
	$workshops = get_workshop_info($conf_id);
	
	// break data up into left column and right column
	$left_table = '<ul>';
	$right_table = '<ul>';

	$left = 1;
	if (count($workshops) > 0) {
		foreach ($workshops as $wshop) {
			if (isset($wshop['name'])) {
				$cell = '<li style="font-weight:bold; color: #2F5376;">' . $wshop['name'] . ' (' . $wshop['count'] . '/' . $wshop['max_attend'] . ")</li>\n<ul>\n";
				foreach ($wshop['attendee'] as $user) {
					$cell .= '<li>' . $user . "</li>\n";
				}
				$cell .= "</ul>\n";
				if ($left == 1) {
					$left_table .= $cell;
				} else {
					$right_table .= $cell;
				}
				// toggle $left between 1 and zero
				$left = 1 - $left;
			}
		}
		$left_table .= "</ul>\n";
		$right_table .= "</ul>\n";
		$html_output .= '<table><td width="270">' . $left_table . "</td><td>" . $right_table . "</td></table>";
	} else {
		$html_output .= "No workshops defined yet.";
	}

	return $html_output;
}

?>
